#!usr/bin/python
#encoding=utf-8

import time
import uuid
import chardet
import requests
from filter_keywords import filter_keywords
from filter_keywords import FkwDict
from wm.etc.config import SOCKET,ALARMTYPE
from wm.lib import monitorlib
from wm.lib import wmalert
from wm.lib.loglib import log_error
from wm.db.TransactionManager import TransactionManager

def get_fkw_dic():
    '''
    '''
    fkw_dict = FkwDict(1600)

    try:
        tran = TransactionManager()
        try:
            tran.startTransaction()
            sql = "select keyWord from keywords"
            res = tran.select(sql)
            tran.commitTransaction()
            
            if res:
                for row in res:
                    keyword = "".join(row)
                    fkw_dict.add_word(keyword.encode("utf-8"))
            return fkw_dict
        except:
            tran.rollbackTransaction()
        finally:
            tran.close()
            
    except Exception as e:
        log_error("get_fkw_dic():"+str(e))

def sensitive_words_check(row,fkw_dic):
    '''
    sensitive words
    '''
    try:
        response = requests.get(row[1])
        content = response.content
      
        r = chardet.detect(content)
        if r and r['encoding'].upper() != 'UTF-8':
            content = content.decode(r["encoding"])
            content = content.encode('utf-8')
        result = filter_keywords(fkw_dic, content)
        tran = TransactionManager()
        try:
            if result:
                alarmcontent = ":".join(result)      
                alarmid = uuid.uuid1().__str__().replace("-","") 
                alarmcontent = alarmcontent.decode('utf-8')
                sql = "insert into alarmevent(id,createDate,modifyDate,alertCategory,alertContent,occurrenceDate,"+\
                                    "status,websiteName,websiteConfigInfo_id) "+\
                                    "values('" + alarmid + "'," + " CURRENT_TIMESTAMP()," + " CURRENT_TIMESTAMP()," +\
                                    str(ALARMTYPE['SENSITIVE_WORDS_ALARM']) + ",'"+ alarmcontent + "'," + "CURRENT_TIMESTAMP()," + str(0) + ",'" + row[2] + "','"+ row[0] + "')"
    
                tran.execute(sql)
                monitorlib.update_recent_alarmevent(tran,row[0],row[2],str(ALARMTYPE['SENSITIVE_WORDS_ALARM']),alarmcontent)
                wmalert.website_alert(alarm_type=ALARMTYPE['SENSITIVE_WORDS_ALARM'],websiteid=row[0],websitename=row[2],url=row[1],content=alarmcontent)
                
                sql = "insert into sensitivewords(id,createDate,modifyDate,word,alarmEvent_id,websiteConfigInfo_id) "+\
                                "values(replace(uuid(),'-','')," + "CURRENT_TIMESTAMP(),CURRENT_TIMESTAMP()" +\
                                ",'" + alarmcontent + "','" + str(alarmid) + "','" + row[0] + "')"   
                tran.execute(sql)
                print sql
            tran.commitTransaction()
        except:
            tran.rollbackTransaction()
        finally:
            tran.close()     
    except Exception as e:
        log_error("sensitive_words_check():"+str(e))
        print str(e)
        time.sleep(SOCKET['INTERVAL'])


                 
if __name__=="__main__":
    #sensitive_words_check()
    #get_keywords_list()
    #print dir(filter_keywords)
    #fkw_dict = FkwDict(10)
    #fkw_dict.add_word(u"叮当猫".encode("utf-8")) 
    #print fkw_dict.search_word(u"叮当猫".encode("utf-8"))
    #print fkw_dict.search_word(u"叮当猫2".encode("utf-8"))
    #fkw_dict.add_word(u"处女".encode("utf-8"))
    #fkw_dict.add_word(u"口交".encode("utf-8")) 
    #text = u"工信处女干事每月经过下属科室都要亲口交代24口交换机等技术性器件的安装工作 "
    #result = filter_keywords(fkw_dict, text.encode("utf-8"))
    #print type(result)
    #print " ".join([str(unicode(item, "utf-8")) for item in result])
    get_fkw_dic()
